using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Camalon.ColorPicker
{
	/// <summary>
	/// Summary description for ColorTraker.
	/// </summary>
    [ToolboxItem(false)]
	public class ColorTrackerCmyk : ColorTrackerBase //System.Windows.Forms.TrackBar
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ColorTrackerCmyk():base()
		{
	
			InitializeComponent();

			base.Maximum = 100;
			base.Minimum = 0;
			base.Vertical = false;


			// TODO: Add any initialization after the InitializeComponent call
			ColorRenderingValue.CmykColorRangeChanged+=new EventHandler(ColorRenderingHelper_CmykColorRangeChanged);
			ColorRenderingValue.ValueChanged+=new EventHandler(ColorRenderingHelper_ValueChanged);
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this)).EndInit();

		}
		#endregion

		private void ColorRenderingHelper_CmykColorRangeChanged(object sender, EventArgs e)
		{

			if( _eventsLocked) return;
			base.Maximum = 100;
			base.Minimum = 0;

			
			CMYK cmyk =  ColorRenderingValue.CMYK;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Cyan)		base.Value = cmyk.Cyan;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Magenda)	base.Value = cmyk.Magenda;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Yellow)	base.Value = cmyk.Yellow;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Key)		base.Value = cmyk.Key;

			this.Invalidate();

		}


		private void ColorRenderingHelper_ValueChanged(object sender, EventArgs e)
		{
			if( _eventsLocked) return;

			//if( ColorRenderingValue.OpacityEnabled) { this.Invalidate();return;}

			CMYK cmyk =  ColorRenderingValue.CMYK;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Cyan)		base.Value = cmyk.Cyan;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Magenda)	base.Value = cmyk.Magenda;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Yellow)	base.Value = cmyk.Yellow;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Key)		base.Value = cmyk.Key;

			this.Invalidate();
		}



		private bool _eventsLocked;
		protected override void ResetColorRenderingValue()
		{
			if( _eventsLocked) return;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.None) return;

			_eventsLocked = true;
			
			int value = (int)Math.Round( (double)this.Value );

			CMYK cmyk =  ColorRenderingValue.CMYK;

			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Cyan)		cmyk.Cyan		= value;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Magenda)	cmyk.Magenda	= value;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Yellow)	cmyk.Yellow		= value;
			if( ColorRenderingValue.CmykColorRange == CmykColorRange.Key)		cmyk.Key		= value;

			ColorRenderingValue.CMYK = cmyk;

			this.Invalidate();

			_eventsLocked = false;

		}


		#region DRAWING



		protected override  void UpdateSliderColor( Graphics graphics, Rectangle colorRegion)
		{
			if( graphics == null) throw new ArgumentNullException("graphics","Graphics object can not be null");

			CmykColorRange cr = ColorRenderingValue.CmykColorRange;
			if( cr == CmykColorRange.None) return;

			Color startColor;
			Color endColor  = Color.White;


//			CMYK cmyk = ColorRenderingValue.CMYK;
//			RGB rgbStart;
//			RGB rgbEnd;
				
			switch ( cr ) 
			{
					
				case CmykColorRange.Cyan:
				{
					startColor = Color.Cyan;

//					CMYK cmykStart	= cmyk;	cmyk.Cyan = 0;
//					CMYK cmykEnd	= cmyk;	cmyk.Cyan = 100;
//					rgbStart	= ColorConverter.CMYKtoRGB(cmykStart);
//					rgbEnd		= ColorConverter.CMYKtoRGB(cmykEnd);
					break;
				}
				case CmykColorRange.Magenda:
				{
					startColor = Color.Magenta;

//					CMYK cmykStart	= cmyk;	cmyk.Magenda = 0;
//					CMYK cmykEnd	= cmyk;	cmyk.Magenda = 100;
//					rgbStart	= ColorConverter.CMYKtoRGB(cmykStart);
//					rgbEnd		= ColorConverter.CMYKtoRGB(cmykEnd);
					break;
				}
				case CmykColorRange.Yellow:
				{
					startColor = Color.Yellow;

//					CMYK cmykStart	= cmyk;	cmyk.Yellow = 0;
//					CMYK cmykEnd	= cmyk;	cmyk.Yellow = 100;
//					rgbStart	= ColorConverter.CMYKtoRGB(cmykStart);
//					rgbEnd		= ColorConverter.CMYKtoRGB(cmykEnd);
					break;
				}
				default://CmykColorRange.Key
				{
					startColor = Color.Black;

//					CMYK cmykStart	= cmyk;	cmyk.Key = 0;
//					CMYK cmykEnd	= cmyk;	cmyk.Key = 100;
//					rgbStart	= ColorConverter.CMYKtoRGB(cmykStart);
//					rgbEnd		= ColorConverter.CMYKtoRGB(cmykEnd);
					break;
				}
			}

//			startColor	= Color.FromArgb( rgbStart.Red, rgbStart.Green, rgbStart.Blue );
//			endColor	= Color.FromArgb( rgbEnd.Red,	rgbEnd.Green,	rgbEnd.Blue );

			using ( LinearGradientBrush lgb = new LinearGradientBrush( colorRegion, endColor,startColor, 180f ) ) 
				graphics.FillRectangle( lgb, colorRegion );

		}

		#endregion DRAWING

	}
}
